package com.xczl.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.xczl.demo.dao.GoodsCateDao;
import com.xczl.demo.dao.GoodsDao;
import com.xczl.demo.domain.GoodsCate;

/**
 * 商品分类服务类
 * 
 * @author DMJ
 *
 */
@Service
@Transactional
public class GoodsCateService {

	@Autowired
	private GoodsCateDao goodsCateDao;

	@Autowired
	private GoodsDao goodsDao;

	/**
	 * 添加商品分类
	 * 
	 * @param goodsCate
	 */
	public void add(GoodsCate goodsCate) {
		Assert.notNull(goodsCate, "分类实体不存在");
		Assert.hasText(goodsCate.getName(), "请填写分类名称");
		goodsCateDao.insertGoodsCate(goodsCate);
	}

	/**
	 * 删除商品分类
	 * 
	 * @param id
	 */
	public void delete(int id) {
		long count = goodsDao.countByCateId(id);
		Assert.isTrue(count == 0, "商品分类以下存在商品");
		goodsCateDao.deleteGoodsCate(id);
	}

	/**
	 * 更新商品分类
	 * 
	 * @param goodsCate
	 */
	public void update(GoodsCate goodsCate) {
		Assert.notNull(goodsCate, "商品分类实体不存在");
		Assert.hasText(goodsCate.getName(), "请填写分类名称");
		Assert.notNull(goodsCate.getCateId(), "商品分类ID不存在");
		goodsCateDao.updateGoodsCate(goodsCate);
	}
}
